import db from './db';
import cfg from '../../etc/cfg';

export function getClassAndSeg(isactive) {
    return db.get('dic_mr_segement_class')
        .where(parseInt(isactive) === 2 ? {language: cfg.lang} : {IsActive: isactive, language: cfg.lang})
        .orderBy('ORDER_ID')
        .list({
            columns: parseInt(isactive) === 2 ? [] : ['code', 'name', 'input'], withRelated: [{
                segTypes: (qb) => {
                    qb.where(parseInt(isactive) === 2 ? {} : {IsActive: isactive}).orderBy('order_id').column('code', 'name', 'input', 'class_code');
                }
            }]
        });
}
export function getElementBySegCode(segcode, isactive) {
    return db.get('dic_mr_segement')
        .where(parseInt(isactive) === 2 ? {language: cfg.lang} : {IsActive: isactive, language: cfg.lang})
        .where({code: segcode})
        .orderBy('order_id')
        .list({
            columns: parseInt(isactive) === 2 ? [] : ['code', 'name', 'input'],
            withRelated: [{
                elements: (qb) => {
                    qb.where(parseInt(isactive) === 2 ? {} : {
                        'dic_mr_segement_element.IsActive': isactive
                    }).orderBy('order_id');
                }
            }]
        });
}
export function getValuesByEleCode(elecode, isactive) {
    return db.get('dic_mr_element_value')
        .query(qb => {
            if (parseInt(isactive) != 2) {
                qb.where({IsActive: isactive});
            }
            qb.where({element_type_code: elecode}).column(['code', 'input', 'element_type_code', 'name', 'order_id', 'isactive']);
        }).list();
}
